<!--
 * @Description: 发货
 * 
 * @Author: XiongYaoYuan<xiongyaoyuan>
 * @Date: 2023-07-18 11:15:41
 * @FilePath: /yuxiuproweb/src/views/mes/pro/invoice/mods/send.vue
-->
<template>
  <el-dialog
    :close-on-click-modal="false"
    v-dialogDrag
    title="发货"
    :visible.sync="shows"
    width="500px"
    append-to-body
    v-loading="loading"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="86px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="发货单号" prop="trackingNumber">
            <el-input
              v-model="form.trackingNumber"
              placeholder="请输入发货单号"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="shows = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { setNumber } from "@/api/asc/invoice";
import { getInfo, getNumber } from "@/api/asc/invoice";

export default {
  props: {
    show: Boolean,
    type: String,
    data: {
      type: Array,
      default: () => [],
    },
    selection: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      loading: false,
      // 表单参数
      form: {
        trackingNumber: "",
      },
      // 表单校验
      rules: {
        trackingNumber: [
          { required: true, message: "请输入发货单号", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    shows: {
      get() {
        return this.show;
      },
      set(newValue) {
        this.$emit("update:show", newValue);
      },
    },
  },
  methods: {
    // 勾选全部查询
    async setSelection() {
      const list2 = await Promise.all(
        this.selection?.map(async (e) => {
          const res = await getInfo({
            orderId: e.proOrderId,
          });

          const customCurtainList = res.data?.customCurtainList || [];
          const proOrderCustomWindowCurtains =
            res.data?.proOrderCustomWindowCurtains || [];
          const proOrderCustomOthers = res.data?.proOrderCustomOthers || [];
          const proOrderRaws = res.data?.proOrderRaws || [];

          return [
            ...customCurtainList.map((c) => {
              return {
                orderId: `${e.proOrderId}`,
                productId: `${c.proOrderCustomCurtainId}`,
                type: "0",
                trackingNumber: this.form.trackingNumber,
              };
            }),
            ...proOrderCustomWindowCurtains.map((c) => {
              return {
                orderId: `${e.proOrderId}`,
                productId: `${c.orderCustomWindowCurtainId}`,
                type: "1",
                trackingNumber: this.form.trackingNumber,
              };
            }),
            ...proOrderCustomOthers.map((c) => {
              return {
                orderId: `${e.proOrderId}`,
                productId: `${c.orderCustomOtherId}`,
                type: "2",
                trackingNumber: this.form.trackingNumber,
              };
            }),
            ...proOrderRaws.map((c) => {
              return {
                orderId: `${e.proOrderId}`,
                productId: `${c.orderRawId}`,
                type: "3",
                trackingNumber: this.form.trackingNumber,
              };
            }),
          ];
        })
      );
      return list2.flat();
    },

    // 设置订单状态
    async setOrderState(list) {
      const list1 = [...new Set((list || []).map((e) => e.orderId))];
      const list2 = await Promise.all(
        list1?.map(async (orderId) => {
          const res = await getInfo({
            orderId,
          });

          const customCurtainList = res.data?.customCurtainList || [];
          const proOrderCustomWindowCurtains =
            res.data?.proOrderCustomWindowCurtains || [];
          const proOrderCustomOthers = res.data?.proOrderCustomOthers || [];
          const proOrderRaws = res.data?.proOrderRaws || [];

          const l1 = customCurtainList?.filter((e) => !!e.trackingNumber);
          const l2 = proOrderCustomWindowCurtains?.filter(
            (e) => !!e.trackingNumber
          );
          const l3 = proOrderCustomOthers?.filter((e) => !!e.trackingNumber);
          const l4 = proOrderRaws?.filter((e) => !!e.trackingNumber);

          if (l1.length + l2.length + l3.length + l4.length === 0) {
            return {
              orderId,
              state: 0,
            };
          }
          if (
            l1.length + l2.length + l3.length + l4.length !==
            customCurtainList.length +
              proOrderCustomWindowCurtains.length +
              proOrderCustomOthers.length +
              proOrderRaws.length
          ) {
            return {
              orderId,
              state: 1,
            };
          } else {
            return {
              orderId,
              state: 2,
            };
          }
        })
      );

      const list3 = await Promise.all(
        list2.map(async (e) => {
          if (e.state === 2) {
            const res = await getNumber({
              orderId: e.orderId,
            });
            return true;
          }
          return true;
        })
      );
      this.loading = false;
      this.$modal.msgSuccess("操作成功");
      this.$emit("update");
      this.shows = false;
    },

    /** 提交按钮 */
    async submitForm() {
      try {
        await this.$refs["form"].validate();
        this.$confirm(`是否确定发货？`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(async () => {
          this.loading = true;
          const list0 = await this.setSelection();

          this.data.forEach((e) => {
            const cell = list0?.find(
              (c) =>
                `${c.orderId}` === `${e.orderId}` &&
                `${c.productId}` === `${e.productId}`
            );
            if (!cell) {
              list0.push({
                ...e,
                trackingNumber: this.form.trackingNumber,
              });
            }
          });
          await setNumber(list0);
          await this.setOrderState(list0);
        });
      } catch (e) {
        console.log(e);
      }
    },
  },
};
</script>
